package com.chris.homecare.screens
{
	import com.chris.homecare.AppScreen;
	import com.chris.homecare.TextureAtlasManager;
	import com.chris.homecare.components.menu.MenuListComponent;
	import com.chris.homecare.data.DataUtil;
	import com.chris.homecare.data.FetchRemoteData;
	import com.chris.homecare.utils.CommonUtils;
	
	import feathers.controls.Header;
	import feathers.controls.Screen;
	import feathers.layout.VerticalLayout;
	
	import starling.display.DisplayObject;
	import starling.display.Image;
	import starling.display.Quad;
	import starling.events.Event;
	
	public class MenuScreen extends Screen
	{
		private var header:Header;
		private var logoImg:Image;
		private var menuCollection:Array;
		private var infoCollection:Array;
		private var menuBtnsArray:Array = new Array();
		private var subMenuBtnsArray:Array = new Array();
		
		private var eventsIcon:Image;
		private var jobsIcon:Image;
		private var locationIcon:Image;
		private var ourServicesIcon:Image;
		private var requestIcon:Image;
		private var updatesIcon:Image;
		
		public function MenuScreen()
		{
			super();
			
			eventsIcon = new Image(TextureAtlasManager.getAtlas().getTexture("events_menu"));
			jobsIcon = new Image(TextureAtlasManager.getAtlas().getTexture("jobs_menu"));
			locationIcon = new Image(TextureAtlasManager.getAtlas().getTexture("location_menu"));
			ourServicesIcon = new Image(TextureAtlasManager.getAtlas().getTexture("ourServices_menu"));
			requestIcon = new Image(TextureAtlasManager.getAtlas().getTexture("request_menu"));
			updatesIcon = new Image(TextureAtlasManager.getAtlas().getTexture("updates_menu"));
			
			menuCollection = [{color:0xF59C44, heading:"Our Services", img:ourServicesIcon, page:"Info"}, {color:0x84Cf6E, heading:"Events", img:eventsIcon, page:"Event"}, {color:0x6699D5, heading:"Updates", img:updatesIcon, page:"UpdatesList"}, 
				{color:0xF67171, heading:"Location", img:locationIcon, page:"Location"}, 
				{color:0xA57BC9, heading:"Jobs", img:jobsIcon, page:"JobList"}, {color:0xF2C36F, heading:"Request", img:requestIcon, page:"Request"}];
			
			infoCollection = [{page:"Business", color:0x353535}, {page:"Services", color:0x272727}, {page:"Team", color:0x464646}];
		}
		
		override protected function screen_addedToStageHandler(event:Event):void
		{
			AppScreen.makeGestureUnAvailable();
			super.screen_addedToStageHandler(event);
		}
		
		
		override protected function initialize():void
		{
			FetchRemoteData.getClient().getPatientList(DataUtil.getUserEmail());
			this.layout = new VerticalLayout();
			
			header = new Header();
			header.paddingTop = header.paddingBottom = 0;
			logoImg = new Image(TextureAtlasManager.getAtlas().getTexture("speiuss"));
			header.centerItems = new <DisplayObject> [logoImg];
			addChild(header);
			header.backgroundSkin = new Quad(10, 10, 0x353535);
			//header.backgroundSkin = new Quad(1.1,0xFF0000);
			header.paddingRight = 20;
			
			for(var i:int=0; i<menuCollection.length; i++)
			{
				var menuListComp:MenuListComponent = new MenuListComponent(menuCollection[i]);
				addChild(menuListComp);
				menuBtnsArray.push(menuListComp);
				menuListComp.addEventListener(MenuListComponent.CLOSE_SUBMENU_ITEMS, onCloseSubMenuItmes);
				
				if(menuCollection[i].page == "Info")
				{
					menuListComp.addEventListener(MenuListComponent.SHOW_SUBMENU_ITEMS, onShowSubMenuItems);
					for(var j:int=0; j<infoCollection.length; j++)
					{
						var subMenu:MenuListComponent = new MenuListComponent(infoCollection[j], true);
						addChild(subMenu);
						subMenuBtnsArray.push(subMenu);
						subMenu.includeInLayout = false;
						subMenu.visible = false;
					}
				}
			}
		}
		
		private function onCloseSubMenuItmes():void
		{
			for(var j:int=0; j<subMenuBtnsArray.length; j++)
			{
				subMenuBtnsArray[j].includeInLayout = false;
				subMenuBtnsArray[j].visible = false;
			}
			MenuListComponent.isSubMenuOpened = false;
		}
		
		private function onShowSubMenuItems():void
		{
			for(var j:int=0; j<subMenuBtnsArray.length; j++)
			{
				subMenuBtnsArray[j].includeInLayout = true;
				subMenuBtnsArray[j].visible = true;
			}
			MenuListComponent.isSubMenuOpened = true;
		}
		
		private function onEventComplete(event:Event):void
		{
			this.dispatchEventWith(Event.COMPLETE,true,event.data);
		}
		
		override protected function draw():void
		{
			super.draw();
			var availHeight:Number = this.height - header.height;
			header.width = this.width;
			
			for(var i:int=0; i<menuBtnsArray.length; i++)
			{
				var menuListComp:MenuListComponent = menuBtnsArray[i];
				menuListComp.givenWidth = this.width;
				menuListComp.givenHeight = availHeight/6;
				menuListComp.invalidate(INVALIDATION_FLAG_LAYOUT);
			}
			
			for each(var subMenuItems:MenuListComponent in subMenuBtnsArray)
			{
				subMenuItems.givenWidth = this.width;
				subMenuItems.givenHeight = 80*CommonUtils.scaleValue;
				subMenuItems.invalidate(INVALIDATION_FLAG_LAYOUT);
			}
		}
	}
}